package com.ycsj.goldmedalnewconcept.utils;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import com.ycsj.goldmedalnewconcept.view.magicindicator.MagicIndicator;
import com.ycsj.goldmedalnewconcept.view.magicindicator.ViewPagerHelper;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.UIUtil;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.commonnavigator.CommonNavigator;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

/**
 * 创建日期：2017/8/31  13:34
 * 描述:封装的导航条的管理类
 * 作者:张伟
 */

public class IndicatorManager {

    /**
     * 与ViewPager绑定
     * 设置不滚动均分屏幕的Tab
     *
     * @param context     上下文
     * @param textsize    tab的文字大小,传入0表示使用控件默认的文字大小,非0时即设置文字大小
     * @param mag         MagicIndicator控件
     * @param vp          ViewPager控件,如果不需要绑定ViewPager时传入null即可
     * @param mtitles     Tab的标题集合
     * @param normalColor 没有被选中时的文字颜色
     * @param selectColor 被选中的文字颜色
     * @param listener    被选中时的回调监听方法
     */
    public static void setIndicator(Context context, final int textsize, MagicIndicator mag, final ViewPager vp, final String[] mtitles, final int normalColor, final int selectColor, final onSelectedListener listener) {

        CommonNavigator commonNavigator = new CommonNavigator(context);
        //设置模式
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mtitles == null ? 0 : mtitles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                //设置Tab没有被选中的颜色
                colorTransitionPagerTitleView.setNormalColor(normalColor);
                //设置Tab被选中的颜色
                colorTransitionPagerTitleView.setSelectedColor(selectColor);
                //设置Tab的文字
                colorTransitionPagerTitleView.setText(mtitles[index]);
                //设置字体大小
                if (textsize != 0) {
                    colorTransitionPagerTitleView.setTextSize(UIUtil.dip2sp(context, textsize));
                }
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (vp != null) {
                            vp.setCurrentItem(index);
                        }
                        //设置回调监听
                        if (listener != null) {
                            listener.onSelected(index);
                        }
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                //设置动画效果
                indicator.setEndInterpolator(new DecelerateInterpolator());
                //设置线的高度
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                //设置导航条的偏移位置
//                indicator.setYOffset(UIUtil.dip2px(context, 39));
                //设置导航条的模式
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                //设置导航条的颜色
                indicator.setColors(selectColor);
                return indicator;
            }
        });
        mag.setNavigator(commonNavigator);
        if (vp != null) {
            ViewPagerHelper.bind(mag, vp);
        }
    }


    /**
     * 设置滚动的Tab
     *
     * @param flag        false和true都可,只是一个配置Tab滚动的标志
     * @param context
     * @param mag         ViewPager控件,如果不需要绑定ViewPager时传入null即可
     * @param mtitles
     * @param normalColor
     * @param selectColor
     * @param listener    被选中时的回调监听方法
     */
    public static void setIndicator(boolean flag, Context context, MagicIndicator mag, final ViewPager vp, final String[] mtitles, final int normalColor, final int selectColor, final onSelectedListener listener) {

        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mtitles == null ? 0 : mtitles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mtitles[index]);
                simplePagerTitleView.setNormalColor(normalColor);
                simplePagerTitleView.setSelectedColor(selectColor);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (vp != null) {
                            vp.setCurrentItem(index);
                        }
                        //设置回调监听
                        if (listener != null) {
                            listener.onSelected(index);
                        }
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                //设置线的高度
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(selectColor);
                return indicator;
            }
        });
        mag.setNavigator(commonNavigator);
        if (vp != null) {
            ViewPagerHelper.bind(mag, vp);
        }
    }


    /**
     * MagicIndicator选中位置的监听
     */
    public interface onSelectedListener {
        void onSelected(int pos);
    }
}
